﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using JM.JCY.BusinessEntity;
using JM.JCY.Proxy;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Common;
using JM.JCY.Common.Utilities;
using Microsoft.SharePoint;

namespace JM.JCY.SharePointComponent.IncorruptArchives.StaffWeddingApprovalDetails
{
    public partial class StaffWeddingApprovalDetailsUserControl : UserControl
    {
        public int _caseId
        {
            set { ViewState["_caseId"] = value; }
            get { return (int)ViewState["_caseId"]; }
        }

        public string _isApproval
        {
            set { ViewState["_isApproval"] = value; }
            get { return (string)ViewState["_isApproval"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (IncorruptProxy proxy = new IncorruptProxy())
                {
                    if (Request.QueryString["CaseId"] != null)
                    {
                        _caseId = Int32.Parse(Request.QueryString["CaseId"].ToString());
                        _isApproval = Request.QueryString["IsApproval"].ToString();
                    }

                    var loadDT = proxy.GetStaffWeddingApproval(_caseId, string.Empty);
                    Session["EditStaffWeddingApprovalDT"] = loadDT;
                    LoadForView(loadDT);
                    LoadForEdit(loadDT);
                }

            }

            // switch controls
            if (this.hfIsEdit.Value != "1")
                SwitchControls(true, false);
            else
                SwitchControls(false, true);
        }

        private void SwitchControls(bool isLabelsShow, bool isControlsShow)
        {
            //Labels
            this.lblAddress.Visible = isLabelsShow;
            this.lblApplicant.Visible = isLabelsShow;
            this.lblApproval.Visible = isLabelsShow;
            this.lblApprovalComments.Visible = isLabelsShow;
            this.lblHoldAddress.Visible = isLabelsShow;
            this.lblHoldRange.Visible = isLabelsShow;
            this.lblONumber.Visible = isLabelsShow;
            this.lblOYear.Visible = isLabelsShow;
            this.lblNumber.Visible = isLabelsShow;
            this.lblPeopleCount.Visible = isLabelsShow;
            this.lblReason.Visible = isLabelsShow;
            this.lblTableCount.Visible = isLabelsShow;
            this.lblApprovalDate.Visible = isLabelsShow;


            // Controls
            this.txtAddress.Visible = isControlsShow;
            this.txtApplicant.Visible = isControlsShow;
            this.txtApproval.Visible = isControlsShow;
            this.txtApprovalComments.Visible = isControlsShow;
            this.txtHoldAddress.Visible = isControlsShow;
            this.txtHoldRange.Visible = isControlsShow;
            this.lblONumber.Visible = isControlsShow;
            this.lblOYear.Visible = isControlsShow;
            this.txtYear.Visible = isControlsShow;
            this.txtNumber.Visible = isControlsShow;
            this.txtPeopleCount.Visible = isControlsShow;
            this.txtReason.Visible = isControlsShow;
            this.txtTableCount.Visible = isControlsShow;
            this.dtApprovalDate.Visible = isControlsShow;
            this.hr5.Visible = isControlsShow;
            this.TBSubmit.Visible = isControlsShow;

        }

        private void LoadForEdit(IncorruptDataSet.T_StaffWedding_ApprovalDataTable loadDT)
        {
            var loadRow = loadDT[0];

            this.txtAddress.Text = loadRow.Address;
            this.txtApplicant.Text = loadRow.Applicant;
            this.txtApproval.Text = loadRow.Approval;
            this.txtApprovalComments.Text = loadRow.Approval_Comment;
            this.txtHoldAddress.Text = loadRow.HoldAddress;
            this.txtHoldRange.Text = loadRow.HoldRange;
            this.txtYear.Text = loadRow.Year;
            this.txtNumber.Text = loadRow.Number;
            this.txtPeopleCount.Text = loadRow.People_Count;
            this.txtReason.Text = loadRow.Reason;
            this.txtTableCount.Text = loadRow.Table_Count;
            if (!loadRow.ApprovalDate.Equals(Convert.ToDateTime(JCYConstant.nullDate)))
                this.hfApprovalDate.Value = loadRow.ApprovalDate.ToString();
            else
                this.hfApprovalDate.Value = string.Empty;
        }

        private void LoadForView(IncorruptDataSet.T_StaffWedding_ApprovalDataTable loadDT)
        {
            var loadRow = loadDT[0];

            this.lblAddress.Text = loadRow.Address;
            this.lblApplicant.Text = loadRow.Applicant;
            this.lblApproval.Text = loadRow.Approval;
            this.lblApprovalComments.Text = loadRow.Approval_Comment;
            this.lblHoldAddress.Text = loadRow.HoldAddress;
            this.lblHoldRange.Text = loadRow.HoldRange;
            this.lblNumber.Text = loadRow.Year + "年" + loadRow.Number + "号";
            this.lblPeopleCount.Text = loadRow.People_Count;
            this.lblReason.Text = loadRow.Reason;
            this.lblTableCount.Text = loadRow.Table_Count;
            if (!loadRow.ApprovalDate.Equals(Convert.ToDateTime(JCYConstant.nullDate)))
                this.lblApprovalDate.Text = loadRow.ApprovalDate.ToString(JCYConstant.Date_Format_CN);

        }

        protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
        {
            using (IncorruptProxy proxy = new IncorruptProxy())
            {
                if (Session["EditStaffWeddingApprovalDT"] != null)
                {
                    var editDT = (IncorruptDataSet.T_StaffWedding_ApprovalDataTable)Session["EditStaffWeddingApprovalDT"];
                    var editRow = editDT[0];
                    editRow.Year = this.txtYear.Text;
                    editRow.Number = this.txtNumber.Text;
                    editRow.Applicant = this.txtApplicant.Text;
                    editRow.Address = this.txtAddress.Text;
                    editRow.Reason = this.txtReason.Text;
                    editRow.HoldRange = this.txtHoldRange.Text;
                    editRow.HoldAddress = this.txtHoldAddress.Text;
                    editRow.Table_Count = this.txtTableCount.Text;
                    editRow.People_Count = this.txtPeopleCount.Text;

                    editRow.Approval_Comment = this.txtApprovalComments.Text;
                    editRow.Approval = this.txtApproval.Text;
                    if (!this.dtApprovalDate.IsDateEmpty)
                        editRow.ApprovalDate = this.dtApprovalDate.SelectedDate;
                    else
                        editRow.ApprovalDate = Convert.ToDateTime(JCYConstant.nullDate);

                    JCYUtility.UpdateCommonFields(editRow, "Iori");

                    proxy.UpdateStaffWeddingApproval(editDT);
                    Response.Redirect("StaffWeddingApproval.aspx");

                }
            }
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("StaffWeddingApproval.aspx");
        }

        protected void lkbEditStaffWeddingApproval_Click(object sender, EventArgs e)
        {
            this.hfIsEdit.Value = "1";
            SwitchControls(false, true);
            if (_isApproval == "1")
            {
                this.txtApproval.Enabled = true;
                this.dtApprovalDate.Enabled = true;
                this.txtApprovalComments.Enabled = true;
            }

            else
            {
                this.txtApproval.Enabled = false;
                this.dtApprovalDate.Enabled = false;
                this.txtApprovalComments.Enabled = false;
            }

            if (!this.hfApprovalDate.Value.Equals(string.Empty))
                this.dtApprovalDate.SelectedDate = Convert.ToDateTime(this.hfApprovalDate.Value);
            else
                this.dtApprovalDate.ClearSelection();

        }
    }
}
